package com.project.controller;

import com.project.bean.BuyBean;
import com.project.bean.BuyCarBean;
import com.project.bean.ProductBean;
import com.project.bean.SuppliersBean;
import com.project.config.UploadUtil;
import com.project.service.IBuyService;
import com.project.service.ISuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("buy")
public class BuyController {
    @Autowired
    private IBuyService service;
    @Autowired
    private ISuppliersService suppliersService;
    @RequestMapping("add")
    public String add(ProductBean product, @RequestParam("img") MultipartFile mf, HttpServletRequest request, @RequestParam("supId") int supId, @RequestParam("num")int number, String method){
        Map<Integer, BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute(supId+"");
        String fileName =  UploadUtil.upload(mf, "static/html/img");
        product.setPhoto(fileName);
        service.add(method, product, shopMap, number);
        System.out.println(shopMap);
        return "ok";
    }

    @RequestMapping("remove")
    public String remove(int productId,int supId,HttpServletRequest request){
        Map<Integer, BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute(supId+"");
        service.del(shopMap, productId);
        return "ok";
    }

    @RequestMapping("sureBuy")
    public String buy(int supId,@RequestParam("userName") String userName, HttpServletRequest request){
//        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");

        //得到当前供应商的购物车map
        Map<Integer, BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute(supId+"");
        System.out.println(userName);
        System.out.println(supId);
        BuyBean buy = new BuyBean(supId, userName);
        service.sureBuy(shopMap,buy);
        return "ok";
    }

    @RequestMapping("findAllSup")
    public List<SuppliersBean> findAll(HttpServletRequest request){
        List<SuppliersBean> supList = suppliersService.findAll();
        for(SuppliersBean s : supList){
            request.getSession().setAttribute(s.getId()+"",   new HashMap<Integer, BuyCarBean>());
        }
        return supList;
    }
//    @RequestMapping("a")
//    public UserBean temp(HttpServletRequest request){
//        request.getSession().setAttribute("loginUser", new UserBean("a", "张三"));
//        return (UserBean) request.getSession().getAttribute("loginUser");
//    }

    @RequestMapping("findBySup")
    public Collection<BuyCarBean> findBySup(int supId, HttpServletRequest request){
        Map<Integer, BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute(supId+"");
        Collection<BuyCarBean> shopProductList =shopMap.values();
        return shopProductList;
    }
    @RequestMapping("updateNum")
    public void updateNum(int supId,int proId,int number,HttpServletRequest request){
        Map<Integer, BuyCarBean> shopMap = (Map<Integer, BuyCarBean>)  request.getSession().getAttribute(supId+"");
        service.updateNum(proId, number, shopMap);
    }





}
